/*
 * @Date: 2023-11-13 15:46:51
 * @LastEditTime: 2024-01-17 17:20:50
 * @Description: content
 */
import Vue from 'vue'

// 防止处理多次点击
function noMultipleClicks(methods, info) {
  // methods是点击后需要执行的函数， info是函数需要传的参数
  let that = this
  if (that.noClick) {
    // 第一次点击
    that.noClick = false
    if ((info && info !== '') || info == 0) {
      // info是执行函数需要传的参数
      methods(info)
    } else {
      methods()
    }
    setTimeout(() => {
      that.noClick = true
    }, 2000)
  } else {
    //  这里是重复点击的判断
  }
}
//导出
export default {
  data: {
    loading: false,
    timer: null
  },

  noMultipleClicks, // 禁止多次点击

  /* 获取日期 */
  getDateTime() {
    const date = new Date()

    const year = date.getFullYear().toString()
    const monty = (date.getMonth() + 1).toString()
    const day = date.getDate().toString()

    return `${year}-${monty}-${day}`
  },

  detailPageTitle(obj) {
    if (!obj) {
      return ''
    }

    const types = {
      1: '一番赏',
      2: '无限赏',
      3: '擂台赏',
      4: '抽卡机',
      5: '积分赏',
      6: '全局赏',
      7: '福利盲盒',
      8: '领主赏',
      9: '连击赏'
    }

    const target = types[obj.type]
    return target
  },

  navTo(item) {
    /* 领券中心 */
    if (item.ttype == 1) {
      this.to({
        url: '/package/index/coupon-center',
        query: {
          coupon_id: item.coupon_id
        }
      })
    }

    if (item.goods_id > 0) {
      if (item.ttype == 2) {
        /* 一番赏 */
        this.to({
          url: '/pages/shouye/detail',
          query: {
            goods_id: item.goods_id
          }
        })
      }

      /* 无限赏 */
      if (item.ttype == 3) {
        this.to({
          url: '/pages/shouye/detail_wuxian',
          query: {
            goods_id: item.goods_id
          }
        })
      }

      /* 连击赏 */
      if (item.ttype == 4) {
        this.to({
          url: '/package/index/lian-ji',
          query: {
            goods_id: item.goods_id
          }
        })
      }
    }
  },

  /**
   * @description: 微信支付(小程序)
   * @param {*}
   * @return {*}
   */
  wxMpPay({ provider = 'weixin', data }) {
    console.log('wxdata', data)
    return new Promise((resolve, reject) => {
      uni.requestPayment({
        provider,
        ...data,
        success: res => {
          // console.log(res)
        },
        fail: err => {
          // console.log('common.wxMpPay-error', err)
        },
        complete: res => {
          console.log('complete (res)', res)
          if (res.errMsg == 'requestPayment:fail cancel') {
            this.toast({
              title: '取消支付',
              icon: 'none',
              duration: 500,
              success: () => {
                /* 取消订单 */
              }
            })

            resolve('cancel')
          }
          if (res.errMsg == 'requestPayment:ok') {
            this.toast({
              title: '支付成功',
              icon: 'success',
              duration: 500,
              success: () => {
                resolve('success')
              }
            })
          }
        }
      })
    })
  },

  /**
   * @description: 规避连点
   * @param {*}
   * @return {*}
   */
  noDouble(fun, data, loading = false) {
    /**
     * $common.noDouble(fun,data)
     * fun  需要规避连点的方法
     * data 该方法需要用到的参数
     * loading  接口加载中状态
     * 传入方法不要写括号
     */

    /* 如果接口加载中,终止任何操作 */
    if (loading) {
      setTimeout(() => {
        uni.showToast({
          title: '请勿重复点击',
          icon: 'none',
          mask: false
        })
      }, 100)
      return
    }

    /* 如果定时器未结束 */
    if (this.data.timer) {
      setTimeout(() => {
        uni.showToast({
          title: '请勿重复点击',
          icon: 'none',
          mask: false
        })
      }, 100)

      /* 清除之前的定时器 */
      clearTimeout(this.data.timer)
      /* 重新设置定时器 */
      this.data.timer = setTimeout(() => {
        this.data.timer = null
      }, 200)
      return
    }

    console.log(data && data)

    /* 设置第一次点击的定时器 */
    this.data.timer = setTimeout(() => {
      this.data.timer = null
    }, 200)

    /* 如果有回调,使用回调并传入参数 */
    fun && fun(data && data)
  },

  /**
   * @description: tost提示
   * @return {*}
   */
  toast(opt) {
    let title = opt.title || ''
    let icon = opt.icon || 'none'
    let mask = opt.mask || false
    let duration = opt.duration || 1500
    let position = opt.position || 'center'

    setTimeout(() => {
      uni.showToast({
        title,
        icon,
        mask,
        duration,
        position,
        success: () => {
          if (opt.success) {
            setTimeout(() => {
              opt.success()
            }, duration)
          }
        }
      })
    }, 100)
  },

  /**
   * @description: 获取规则
   * @param {*} type
   * @return {*}
   */
  getRule(id = '', loading = false) {
    return new Promise((resolve, reject) => {
      Vue.prototype.req({
        url: 'danye',
        Loading: !loading,
        data: {
          type: id
        },
        success: res => {
          if (res.status == 1) {
            if (res.data) {
              res.data = res.data.replace(
                /\<img/gi,
                '<img style="width: 100%;vertical-align: middle;"'
              )
            }

            resolve(res)
          } else {
            reject(res)
          }
        }
      })
    })
  },

  /**
   * @description: 复制到剪切板
   * @param {*} e
   * @return {*}
   */
  copy(e) {
    uni.setClipboardData({
      data: `${e}`
    })
  },

  /**
   * @description: obj转为queryString
   * @param {*} e
   * @return {*}
   */
  qs(e) {
    let str = Object.entries(e)
      .map(([key, value]) => `${key}=${value}`)
      .join('&')
    return `?${str}`
  },

  /**
   * @description: 跳转页面
   * @param {*} type 跳转类型 1 navigate,2 redirect,3 switch,4 relaunch
   * @param {*} url
   * @param {*} query
   * @return {*}
   */
  to({ type = 1, url, query = {}, success, fail, complete }) {
    if (!url) {
      this.toast({
        title: '暂未开放'
      })
      return
    }

    if (Object.keys(query).length > 0) {
      url += this.qs(query)
    }

    const opt = {
      url,
      success,
      fail,
      complete
    }

    const dic = {
      1: uni.navigateTo,
      2: uni.redirectTo,
      3: uni.switchTab,
      4: uni.reLaunch
    }

    const action = dic[type]

    action(opt)
  },

  /**
   * @description: 返回上一页
   * @param {*} e
   * @return {*}
   */
  back(e) {
    /* 判断传入的是否是数字 */
    let pages = getCurrentPages()
    console.log('pgaes', pages)
    if (pages.length >= 2) {
      if (typeof e === 'number' && !isNaN(e)) {
        /* 如果要跳转的长度小于页面栈数量 */
        if (e < pages.length) {
          uni.navigateBack({ delta: e })
          /* 否则回首页 */
        } else {
          uni.switchTab({ url: '/pages/shouye/index' })
        }
        /* 默认返回上一级 */
      } else {
        uni.navigateBack({ delta: 1 })
      }
    } else {
      uni.switchTab({ url: '/pages/shouye/index' })
    }
  },

  /**
   * @description: 提取富文本文字
   * @param {*} val
   * @return {*}
   */
  filterText(val) {
    if (val) {
      return val.replace(
        /[^\u4e00-\u9fa50-9\u3002\uff1f\uff01\uff0c\u3001\uff1b\uff1a\u201c\u201d\u2018\u2019\uff08\uff09\u300a\u300b\u3008\u3009\u3010\u3011\u300e\u300f\u300c\u300d\ufe43\ufe44\u3014\u3015\u2026\u2014\uff5e\ufe4f\uffe5]/g,
        ''
      )
    } else {
      return ''
    }
  }
}
